package com.luhuiguo.cms.client.ui;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FileUploadField;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.FormPanel.Encoding;
import com.extjs.gxt.ui.client.widget.form.FormPanel.Method;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.luhuiguo.cms.client.GlobalConstants;
import com.luhuiguo.cms.client.GlobalMessages;
import com.luhuiguo.cms.client.presenter.EditAttachmentPresenter;

@Singleton
public class EditAttachmentView extends Window implements EditAttachmentPresenter.Display {

    private GlobalConstants constants;

    private GlobalMessages messages;

    private FormPanel formPanel;

    private FormBinding formBinding;

    private FileUploadField fileField;

    private HiddenField<Long> idField;

    private HiddenField<Long> articleIdField;

    private TextField<String> titleField;
    
    private NumberField rankField;
    
    private NumberField gradeField;

    private TextArea descriptionField;

    private Button submitButton;

    private Button cancelButton;

    @Inject
    public EditAttachmentView(GlobalConstants constants, GlobalMessages messages) {
        this.constants = constants;
        this.messages = messages;

        this.setWidth(500);
        this.setHeading(messages.editAttachment());
        this.setAutoHeight(true);
        this.setPlain(true);
        this.setModal(true);

        // this.setBlinkModal(true);
        // setLayout(new FitLayout());

        this.add(createFormPanel());

    }

    private FormPanel createFormPanel() {
        formPanel = new FormPanel();
        formPanel.setHeaderVisible(false);
        formPanel.setBorders(false);
        formPanel.setBodyBorder(false);

        formPanel.setLabelWidth(70);
        formPanel.setAction(GWT.getModuleBaseURL() + "upload");
        formPanel.setEncoding(Encoding.MULTIPART);
        formPanel.setMethod(Method.POST);

        FormData formData = new FormData("95%");

        idField = new HiddenField<Long>();
        idField.setName("id");
        formPanel.add(idField, formData);

        articleIdField = new HiddenField<Long>();
        articleIdField.setName("articleId");
        formPanel.add(articleIdField, formData);

        fileField = new FileUploadField();
        fileField.setName("file");
        fileField.setAllowBlank(false);
        fileField.getMessages().setBrowseText(messages.browse());
        fileField.setFieldLabel(messages.filename());
        formPanel.add(fileField, formData);

        titleField = new TextField<String>();
        titleField.setName("title");
        titleField.setFieldLabel(messages.title());
        titleField.setAllowBlank(false);
        titleField.setMaxLength(constants.maxTitleLength());
        formPanel.add(titleField, formData);
        
        rankField = new NumberField();
        rankField.setPropertyEditorType(Integer.class);
        rankField.setAllowDecimals(false);
        rankField.setName("rank");
        rankField.setFieldLabel(messages.rank());
        formPanel.add(rankField, formData);
        
        gradeField = new NumberField();
        gradeField.setPropertyEditorType(Integer.class);
        gradeField.setAllowDecimals(false);
        gradeField.setName("grade");
        gradeField.setFieldLabel(messages.grade());
        formPanel.add(gradeField, formData);

        descriptionField = new TextArea();
        descriptionField.setName("description");
        descriptionField.setFieldLabel(messages.description());
        descriptionField.setMaxLength(constants.maxStringLength());
        formPanel.add(descriptionField, formData);

        formBinding = new FormBinding(formPanel);
        formBinding.autoBind();

        submitButton = new Button(messages.submit());
        cancelButton = new Button(messages.cancel());
        formPanel.setButtonAlign(HorizontalAlignment.CENTER);
        formPanel.addButton(submitButton);
        formPanel.addButton(cancelButton);

        FormButtonBinding binding = new FormButtonBinding(formPanel);
        binding.addButton(submitButton);

        return formPanel;

    }

    @Override
    public Widget asWidget() {
        return this;
    }

    @Override
    public FormBinding getFormBinding() {
        return formBinding;
    }

    @Override
    public FormPanel getFormPanel() {
        return formPanel;
    }

    @Override
    public Button getSubmitButton() {
        return submitButton;
    }

    @Override
    public Button getCancelButton() {
        return cancelButton;
    }

    @Override
    public FileUploadField getFileField() {
        return fileField;
    }

    @Override
    public TextField<String> getTitleField() {
        return titleField;
    }

}
